package Redis.RedisTemplate;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import Redis.RedisTemplate.bean.Info;

@Component
public class RedisTestor implements ApplicationRunner
{
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

    @Resource(name="redisTemplateA")
    private RedisTemplate redisTemplateA;
    

    @Resource(name="redisTemplateB")
    private RedisTemplate redisTemplateB;
    

    @Resource(name="redisTemplateC")
    private RedisTemplate redisTemplateC;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		// TODO Auto-generated method stub
		
		System.out.println("使用stringRedisTemplate设置stringRedisTemplate键的值为hello stringRedisTemplate");
		stringRedisTemplate.opsForValue().set("stringRedisTemplate", "hello stringRedisTemplate");
		
		System.out.println("使用redisTemplateA设置redisTemplateA键的值为hello redisTemplateA");
		redisTemplateA.opsForValue().set("redisTemplateA", "hello redisTemplateA");

		System.out.println("使用redisTemplateB设置redisTemplateB键的值为hello redisTemplateB");
		redisTemplateB.opsForValue().set("redisTemplateB", "hello redisTemplateB");
		
		
		System.out.println("测试object在redis中的存取");
		
		Info info = new Info();
		info.setInfo1_private("Info1_private");
		info.setInfo2_private("info2_private");
		info.setInfo3_private(3);

		System.out.println("使用redisTemplateA设置redisTemplateA_Obj键的值为info:"+info.toString());
		redisTemplateA.opsForValue().set("redisTemplateA_Obj", info);
		

		System.out.println("使用redisTemplateB设置redisTemplateB_Obj键的值为info");
		redisTemplateB.opsForValue().set("redisTemplateB_Obj", info);
		
		System.out.println("redisTemplateC实例使用的是jdk默认的序列化工具JdkSerializationRedisSerializer");

		System.out.println("使用redisTemplateC设置redisTemplateC_Obj键的值为info");
		try {
		redisTemplateC.opsForValue().set("redisTemplateC_Obj", info);
		
		Info result = (Info)redisTemplateC.opsForValue().get("redisTemplateC_Obj");
		System.out.println(result);
		
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("redisTemplateC操作失败："+e.getMessage());
		}
//		
//		System.out.println("db1中key："+oneRedis.opsForValue().get("key"));
//		System.out.println("db2中key："+twoRedis.opsForValue().get("key"));
		
	}

}
